package cn.com.homeyoda.aimate.aihealth.ui.videoinspect;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.HomeReportListBean;
import cn.com.homeyoda.aimate.aihealth.bean.TiJianRecordBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityTijianRecordBinding;
import cn.com.homeyoda.aimate.aihealth.utils.LoadingStateManager;

import static com.wzq.mvvmsmart.utils.Utils.getContext;


//体检记录
public class TiJianRecordActivity extends BaseActivity<ActivityTijianRecordBinding, VideoInspectModel>{

    private RecordAdapter recordAdapter;
    private List<HomeReportListBean.RowsBean> rowsBeans = new ArrayList<>();
    private String deviceId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public VideoInspectModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(VideoInspectModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        deviceId = getIntent().getStringExtra("deviceId");
        Log.i("huage","deviceId===="+deviceId);
        viewModel.recordData(Build.SERIAL);
        initView();
    }

    private void initView() {
        recordAdapter = new RecordAdapter(R.layout.tijian_record_item,rowsBeans,getApplicationContext());
        binding.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
//        itemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_inset));
//        binding.recordview.addItemDecoration(itemDecoration);
        binding.setAdapter(recordAdapter);
//        binding.recordview.setRecordAdapter(recordAdapter);



       recordAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
           @Override
           public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
               int id = view.getId();
               if(id == R.id.content){
                   Intent intent = new Intent(getContext(), DetectionResultActivity.class);
                   intent.putExtra("reportId", recordAdapter.getData().get(position).getId());
                   intent.putExtra("faceId", recordAdapter.getData().get(position).getFaceId());
                   startActivity(intent);
               }else if(id == R.id.right){
                   viewModel.deleteReport(recordAdapter.getData().get(position).getId());
               }


           }
       });

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        viewModel.uc.finishRefreshing.observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                binding.refreshLayout.finishRefresh();
            }
        });

        viewModel.uc.finishLoadMore.observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                binding.refreshLayout.finishLoadMore();
            }
        });


    }






    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void initViewObservable() {
        viewModel.stateRecordData.observe(this,rowsBeanList -> {
                rowsBeans = rowsBeanList;
                recordAdapter.setNewData(rowsBeans);

        });
        viewModel.deleteReportData.observe(this,baseResponse2 -> {
            if(baseResponse2.getCode() == 200){
                viewModel.recordData(Build.SERIAL);
            }
        });

        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.pageNum++;
                viewModel.recordData(Build.SERIAL);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.pageNum = 1;
                viewModel.recordData(Build.SERIAL);
            }
        });
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_tijian_record;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
